package io.renren.modules.aic.service.impl;

import io.renren.common.utils.DateUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.ProjectConstant;
import io.renren.common.utils.Query;
import io.renren.common.validator.Assert;
import io.renren.modules.aic.dao.MachineGiveDao;
import io.renren.modules.aic.entity.MachineEntity;
import io.renren.modules.aic.entity.MachineGiveEntity;
import io.renren.modules.aic.entity.MemberEntity;
import io.renren.modules.aic.entity.MemberMachineEntity;
import io.renren.modules.aic.service.MachineGiveService;
import io.renren.modules.aic.service.MachineService;
import io.renren.modules.aic.service.MemberMachineService;
import io.renren.modules.aic.service.MemberService;

import java.util.Date;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

@Service("machineGiveService")
public class MachineGiveServiceImpl extends ServiceImpl<MachineGiveDao, MachineGiveEntity> implements MachineGiveService {

	@Autowired
	private MachineService machineService;

	@Autowired
	private MemberService memberService;

	@Autowired
	private MemberMachineService memberMachineService;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		String mobile = (String) params.get("mobile");
		Page<MachineGiveEntity> page = this.selectPage(new Query<MachineGiveEntity>(params).getPage(),
				new EntityWrapper<MachineGiveEntity>().eq(StringUtils.isNotBlank(mobile), "mobile", mobile));
		return new PageUtils(page);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void save(MachineGiveEntity machineGive) {
		MachineEntity machine = machineService.selectById(machineGive.getMachineId());
		Assert.notNull(machine, "矿机不存在");

		MemberEntity member = memberService.selectOne(new EntityWrapper<MemberEntity>().eq("mobile", machineGive.getMobile()));
		Assert.notNull(member, "手机号不存在");

		Date now = new Date();

		// 判断库存
		if (machine.getStock() != -1) {
			Assert.isTrue(machineGive.getQuantity() <= machine.getStock(), "矿机库存不足,剩余库存:" + machine.getStock());
			machine.setStock(machine.getStock() - machineGive.getQuantity());
			machine.setUpdateTime(now);
			machineService.updateById(machine);
		}

		machineGive.setCreateTime(now);
		machineGive.setMemberId(member.getId());
		machineGive.setMachineName(machine.getMachineName());
		this.insert(machineGive);
		for (int i = 0; i < machineGive.getQuantity(); i++) {
			MemberMachineEntity mm = new MemberMachineEntity();
			mm.setMemberId(machineGive.getMemberId());
			mm.setMachineNo("M" + DateUtils.format(new Date(), "yyyyMMddHH") + RandomStringUtils.randomNumeric(5));
			mm.setMachineName(machine.getMachineName());
			mm.setMachineType(machine.getMachineType());
			mm.setPrice(machine.getPrice());
			mm.setHashSecond(machine.getHashSecond());
			mm.setTotalDays(machine.getTotalDays());
			mm.setTotalProfit(machine.getTotalProfit());
			mm.setDayProfit(machine.getDayProfit());
			mm.setRunningDays(0);
			mm.setSumProfit(0d);
			mm.setPreProfit(0d);
			mm.setStatus(ProjectConstant.MACHINE_RUNSTATUS_RUNNING);
			mm.setIsReward(ProjectConstant.COMMON_YES);
			mm.setCreateTime(now);
			mm.setMachineImg(machine.getMachineImg());
			memberMachineService.insert(mm);
		}

		// 计算矿工等级
		Set<Long> ids = new HashSet<Long>();
		ids.add(member.getId());
		memberService.calcUserLevel(ids);
	}
}
